package com.choosefine.it.manager.controller;

import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.choosefine.it.base.BaseController;
import com.choosefine.it.base.dto.PageDTO;
import com.choosefine.it.common.def.ResultCode;
import com.choosefine.it.common.model.CustomRestTemplate;
import com.choosefine.it.common.utils.StringUtils;
import com.choosefine.it.common.utils.URLHandlerUtils;
import com.choosefine.it.manager.model.ManagerEvaluationInfo;
import com.choosefine.it.manager.service.ManagerEvaluationInfoService;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.Page;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * @author 潘钱钦
	下午3:55:44
 *
 */
@RestController
@RequestMapping("/ManagerEvaluationInfo")
@Api(value="项目经理评价信息接口",description="潘钱钦")
public class ManagerEvaluationInfoController extends BaseController{
	@Autowired
	private ManagerEvaluationInfoService managerEvaluationInfoService;
	
	@Autowired
	CustomRestTemplate restTemplate;
	
	@Autowired
	ObjectMapper jsonObjectMapper;
	
	Logger logger=LoggerFactory.getLogger(ManagerEvaluationInfoController.class);
	
	@Value("#{yaml.get('basedata.projectinfo.getProjectInfoByCode')}")
	final String projectinfoURL=null;
	
	@ApiOperation(value = "根据用户的usercode查看评价信息列表", notes = "",response=ManagerEvaluationInfo.class)
	@ApiImplicitParams({@ApiImplicitParam(name = "userCode", value = "用户的唯一编号", required = true, dataType = "String",paramType = "path"),
			@ApiImplicitParam(name = "pageNum", value = "起始行", required = true, dataType = "Integer", paramType = "query"),
			@ApiImplicitParam(name = "pageSize", value = "一页显示的行数", required = true, dataType = "Integer", paramType = "query")})
	@RequestMapping(value="/getManagerEvaluationInfoList/{userCode}",method=RequestMethod.GET)
	public String getManagerEvaluationInfoListByUserCode(@PathVariable String userCode,
			Integer pageNum,Integer pageSize) throws JsonProcessingException{
		String result =null;
		if(StringUtils.isNotBlank(userCode)){
			PageDTO page= new PageDTO();
			page.setPageNum(pageNum);
			page.setPageSize(pageSize);
			Page<ManagerEvaluationInfo> list= managerEvaluationInfoService.selectManagerEvaluationInfoByUserCode(userCode, page);
//			Page<ManagerEvaluationInfo> mList= new Page<ManagerEvaluationInfo>(pageNum, pageSize);
			if(list.size()>0){
				for(ManagerEvaluationInfo m:list){
					String url=String.format(URLHandlerUtils.getUrlBuffer(shixiaobaoURL,  projectinfoURL).toString(),m.getProjectCode());
					Map projectInfoMap=restTemplate.getForObject(url, Map.class, new HashMap<>());
					Map projectInfo = (Map) projectInfoMap.get("data");
					if(projectInfo!=null){
					logger.info("序列化结果 :\n"+jsonObjectMapper.writeValueAsString(projectInfo));
					String projectName = (String) projectInfo.get("projectName");
					String constructionUnit=(String) projectInfo.get("constructionUnit");
					Long projectStart=Long.valueOf(projectInfo.get("projectStart").toString());
					String projectArea=(String) projectInfo.get("projectArea");
					m.setProjectName(projectName);
					m.setProjectStart(projectStart);
					m.setConstructionUnit(constructionUnit);
					m.setProjectArea(projectArea);
//					mList.add(m);
				}
				result= responseSuccess(list);
				}
			}else{
				result= responseFail(ResultCode.FAILED,ResultCode.MSG_DATA_ISEMPTY);
			}
		}else{
			result= responseFail(ResultCode.FAILED,ResultCode.MSG_PARAMS_NULL);
		}
		return result;
	}
}
